//
//  HQualityViewController.m
//  NursingManages
//
//  Created by Zhen Huang on 17/5/26.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HQualityViewController.h"
#import "HSeeRecordViewController.h"
#import "HChangeStandardViewController.h"
#import "HQualityDetailViewController.h"
#import "HDowloadDataViewController.h"
#import "InitDbTable.h"
#import <AFNetworking.h>
#import "Networking.h"
#import <MBProgressHUD.h>
#import "HQcTypeModel.h"
#import "Cell.h"
#import "TreeViewNode.h"
#import "TableQuality.h"
#import "HQc_standardModel.h"
#import <TMCache.h>
#import "HQualityThreeViewController.h"
@interface HQualityViewController ()
<UITableViewDelegate,
UITableViewDataSource>
@property (nonatomic, strong) UISegmentedControl *segment;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, strong) NSMutableArray *nextArray;
@property (nonatomic, strong) NSDictionary *menuParam;
@property (nonatomic, assign) NSInteger menu_id;


@end

@implementation HQualityViewController
{
    //数据源
    NSMutableArray * _dataSource;
    
    //显示的数组
    NSMutableArray * _displayArray;
}

- (instancetype)initWithMemuParam:(NSDictionary *)menuParam
                         menu_id :(NSInteger)menu_id
{
    if (self)
    {
        InitDbTable* ins = [[InitDbTable alloc]init];
        [InitDbTable sharedFMDBManeger];
        self.dataArray = [NSMutableArray new];
        self.nextArray = [NSMutableArray new];
        self.count = [ins QC_TypeCount];
        self.menuParam = menuParam;
        self.menu_id = menu_id;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createSegmentController];
//    [self GetTableCount];
    
    [self.view addSubview:self.tableView];
    NSString *standard_id = [[TMCache sharedCache]objectForKey:@"DEFAULT_QC_STANDARD"];
    if ([standard_id isEqual:[NSNull null]]||standard_id.length == 0) {
        if (self.count != 0) {
        
            [self selectStandard];
            
        }
    }else {
        [self setDataArray];
    }
}

- (void)selectStandard
{
   
    HChangeStandardViewController *HChangSVC = [[HChangeStandardViewController alloc]initWithOpenType:@"presentView" menuParam:self.menuParam];
    HChangSVC.title = @"检查标准";
    HChangSVC.delegate = self;
    UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:HChangSVC];
    [self presentViewController:navc animated:YES completion:nil];

}
 -(void) setDataArray
{
    InitDbTable* ins = [[InitDbTable alloc]init];
    
    [InitDbTable sharedFMDBManeger];
    
    NSString *standard_id = [[TMCache sharedCache]objectForKey:@"DEFAULT_QC_STANDARD"];
    NSMutableArray *array = [ins change:[self.menuParam objectForKey:@"TYPE_ID"]];
    
    if (self.CODE.length > 0) {
        self.dataArray = [ins Qc_code:nil STANDARD_ID:standard_id par_id:@"*" code:self.CODE];
    }else {
        
        if (array.count > 0) {
            HQc_codeModel *model = array[0];
            
            self.dataArray = [ins Qc_code:nil STANDARD_ID:model.STANDARD_ID  par_id:@"*"];

        }else {
            
            self.dataArray = [ins Qc_code:nil STANDARD_ID:standard_id  par_id:@"*"];
        }

    }
    [self.tableView reloadData];

}
- (void)setinitWithHQc_standardModel:(HQc_standardModel *)model
{
    InitDbTable* ins = [[InitDbTable alloc]init];

    if (self.CODE.length > 0) {
        self.dataArray = [ins Qc_code:model.TYPE_ID STANDARD_ID:model.STANDARD_ID par_id:@"*" code:self.CODE];
    }else {
        self.dataArray = [ins Qc_code:model.TYPE_ID STANDARD_ID:model.STANDARD_ID  par_id:@"*"];
        
    }

    [self.tableView reloadData];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
    
}

- (void)GetTableCount
{
    if (self.count == 0) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"还未下载质量检查数据,是否下载?"
                                                        message:nil
                                                       delegate:nil
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"确定", nil];
        [alert show];
        alert.tag = 200;
        alert.delegate = self;

    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 200) {
        switch (buttonIndex) {
                
            case 0:{
                return;
            }
                break;
            case 1:{
                HDowloadDataViewController *HDownLoadVC = [[HDowloadDataViewController alloc]init];
                [HCommon popupMessage:@"正在下载，请稍候"];
                [HDownLoadVC downloadQuality];
            }
                break;
            default:
                break;
        }
    }
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    HQc_codeModel *model = self.dataArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = model.CODE_NAME;
    return cell;
}
- (void)createSegmentController {
    self.segment = [[UISegmentedControl alloc]initWithItems:@[@"查看记录",@"切换标准"]];
    self.segment.frame = CGRectMake(70,5, ScreenWidth-140, 25);
    [self.segment setContentOffset:CGSizeMake(1,0) forSegmentAtIndex:0];
    [self.segment setContentOffset:CGSizeMake(4,0) forSegmentAtIndex:1];
    self.segment.tintColor = [UIColor whiteColor];
    [self Change:self.segment];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor whiteColor]};
    [self.segment setTitleTextAttributes:dic forState:UIControlStateNormal];
    [self.segment addTarget:self action:@selector(Change:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = self.segment;
}

- (void)Change:(UISegmentedControl *)seg {
    NSInteger index = seg.selectedSegmentIndex;
    switch (index) {
        case 0:{
            HSeeRecordViewController *HSeeVC = [[HSeeRecordViewController alloc]initWithLevel_ID:[self.menuParam objectForKey:@"LEVEL_ID"] param:self.menuParam isHnight:0 menu_id:self.menu_id];
            HSeeVC.title = @"检查记录";
            [self.navigationController pushViewController:HSeeVC animated:YES];
        }
            break;
        case 1:{
            HChangeStandardViewController *HCVC = [[HChangeStandardViewController alloc]initWithOpenType:@"pushView" menuParam:self.menuParam];
            HCVC.title = @"检查标准";
            HCVC.delegate = self;
            [self.navigationController pushViewController:HCVC animated:YES];
        }
            break;
        default:
            break;
    }
}

-(void)CallBack:(NSObject *)object{
    
    if ([object isKindOfClass:[HQc_standardModel class]] ){
        HQc_standardModel *model = (HQc_standardModel*)object;
        [self setinitWithHQc_standardModel:model];
        [[TMCache sharedCache]setObject:model.STANDARD_ID forKey:@"DEFAULT_QC_STANDARD"];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HQc_codeModel *model = self.dataArray[indexPath.row];
    InitDbTable* ins = [[InitDbTable alloc]init];
    [InitDbTable sharedFMDBManeger];
    NSMutableArray *array = [ins Qc_code:model.TYPE_ID STANDARD_ID:model.STANDARD_ID par_id:model.CODE_ID];
    
    NSMutableArray *codeDetailArray = [NSMutableArray new];
    for (HQc_codeModel *detailModel in array) {
        NSMutableArray *detailArray = [ins Qc_codeDetail:detailModel.TYPE_ID STANDARD_ID:detailModel.STANDARD_ID par_id:detailModel.CODE_ID];
        NSMutableArray *pushArray = [NSMutableArray new];
        for (HQc_codeModel *Model in detailArray) {
            [pushArray addObject:Model];
        }
        [codeDetailArray addObject:pushArray];
    }
    
    HQualityDetailViewController *HQDetaliVC = [[HQualityDetailViewController alloc]initWithCodeArray:array codeDetailArray:codeDetailArray para:self.menuParam];
    
    HQDetaliVC.score = model.SCORE;
    if ([model.PROBLEM_COLLECTION_FLAG isEqualToString:@"1"]) {
        
        HQDetaliVC.codeModel = model;
        

    }
    HQDetaliVC.rusult_id = [super createPrimaryID];

    HQDetaliVC.title = model.CODE_NAME;
    [self.navigationController pushViewController:HQDetaliVC animated:YES];

    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
